package hibernate

class MtmController {

    def index() {
        render Robot.list()
    }

    def save() {
        def robotA = new Robot([name:"机器人A" + System.currentTimeMillis()])
        robotA.addToRoles(new Role([name:"老师"]))
        robotA.save()

        def robotB = new Robot([name:"机器人B" + System.currentTimeMillis()])
        robotB.addToRoles(new Role([name:"警察"]))
        robotB.addToRoles(new Role([name:"小偷"]))
        robotB.save()

        def robotC = new Robot([name:"机器人C" + System.currentTimeMillis()])
        robotC.addToRoles(new Role([name:"学生"]))
        robotC.save(flush: true)
        render "保存成功"
    }

    def update() {
        def robot = Robot.get(2)
        robot.roles[0].name = "匿名角色"
        robot.save(flush: true)
        render "修改成功"
    }

    def delete() {
        Robot.get(2).delete(flush: true)
        render "删除成功"
    }

}
